package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.business.StorageManEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.AddStorageActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

import io.reactivex.functions.Consumer;

public class AddStorageModel extends BaseViewModel {
    public ObservableField<StorageManEntity> storageManEntity = new ObservableField<>(new StorageManEntity());
    private AddStorageActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 20;
    public int total;

    public AddStorageModel(AddStorageActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    //获取服务范围
    public void getAddressModel(String parentNo) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getDeviceAddress(parentNo)
                .subscribe(new Consumer<BaseEntity<List<AddressEntity>>>() {
                    @Override
                    public void accept(BaseEntity<List<AddressEntity>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
//                        activity.showAreaPop(listBaseEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                activity.showAddressPop(data.getData());
            }
        });
    }

    public void saveStorage() {
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(com.swgk.core.util.SharedPreferenceUtil.REAL_ID, "");
        storageManEntity.get().setCompanyId(companyId);
        DialogHelper.showProgressDialog(activity, null, "数据上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsReleasedAPI().saveStorage(storageManEntity.get()).enqueue(new ICallBack<BaseEntity<Object>>() {

            @Override
            public void onSuccess(BaseEntity<Object> response) {
                activity.showSuccess();
            }
        });
    }

    public void getSelectedAddressLatlng(String provinceNo, String cityNo, String districtNo) {
        NewAPIManager.getInstance().getGoodsReleasedAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new ICallBack<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                activity.fillCurrentAddressLatlng(data.getData());
            }
        });
    }

    public String getNo() {
        return repertory.getNo();
    }
}
